package io.kiki.sba.registry.util;

import org.apache.commons.lang.StringUtils;

import java.util.Collection;
import java.util.Map;
import java.util.Objects;


public final class StringUtil {
    private StringUtil() {
    }

    /**
     * check object not null
     */
    public static void nullValue(Object param, String paraName) {
        if (param == null) {
            throw new IllegalArgumentException(StringFormatter.format("{} is not allowed to be null", paraName));
        }
    }

    public static void nonNullValue(Object param, String paraName) {
        if (param != null) {
            throw new IllegalArgumentException(StringFormatter.format("{} must be null", paraName));
        }
    }


    public static void checkEquals(Object actual, Object expect, String paraName) {
        if (!Objects.equals(actual, expect)) {
            throw new IllegalArgumentException(StringFormatter.format("{}={} is not equals {}", paraName, actual, expect));
        }
    }

    /**
     * check string not blank
     */
    public static void checkNotBlank(String param, String paraName) {
        if (StringUtils.isBlank(param)) {
            throw new IllegalArgumentException(StringFormatter.format("{} is not allowed to be blank", paraName));
        }
    }

    /**
     * check param not empty
     */
    public static void checkNotEmpty(Collection<?> param, String paraName) {
        if (param == null || param.isEmpty()) {
            throw new IllegalArgumentException(StringFormatter.format("{} is not allowed to be empty", paraName));
        }
    }

    /**
     * check param not empty
     */
    public static void checkNotEmpty(Map param, String paraName) {
        if (param == null || param.isEmpty()) {
            throw new IllegalArgumentException(StringFormatter.format("{} is not allowed to be empty", paraName));
        }
    }

    public static void checkNonNegative(long v, String paraName) {
        if (v < 0) {
            throw new IllegalArgumentException(StringFormatter.format("{} is not allowed to be negative, {}", paraName, v));
        }
    }


}
